package cn.iocoder.yudao.module.wine.dal.mysql.supplyorderevaluate;

import java.util.*;

import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.MPJLambdaWrapperX;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.wine.controller.app.supplyorderevaluate.enums.SupplyOrderEvaluateEnum;
import cn.iocoder.yudao.module.wine.dal.dataobject.businesscredit.BusinessCreditDO;
import cn.iocoder.yudao.module.wine.dal.dataobject.businessuser.BusinessUserDO;
import cn.iocoder.yudao.module.wine.dal.dataobject.membertype.MemberTypeDO;
import cn.iocoder.yudao.module.wine.dal.dataobject.supplyorder.SupplyOrderDO;
import cn.iocoder.yudao.module.wine.dal.dataobject.supplyorderevaluate.SupplyOrderEvaluateDO;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.wine.controller.app.supplyorderevaluate.vo.*;

/**
 * 订单评价 Mapper
 *
 * @author 管理员
 */
@Mapper
public interface SupplyOrderEvaluateMapper extends BaseMapperX<SupplyOrderEvaluateDO> {


    /**
     * 我发布的评价列表
     * @param listReqVO
     * @return
     */
    default List<AppSupplyOrderEvaluateRespVO> selectSupplyOrderEvaluateList(AppSupplyOrderEvaluateListReqVO listReqVO){
        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
        //查询我发出的评价
        if (SupplyOrderEvaluateEnum.GIVE_OUT.getCode().equals(listReqVO.getType())){
            return selectJoinList(AppSupplyOrderEvaluateRespVO.class, new MPJLambdaWrapperX<SupplyOrderEvaluateDO>()
                    .eq(SupplyOrderEvaluateDO::getFromUserId, loginUserId)
                    .isNotNull(SupplyOrderEvaluateDO::getOrderId)
                    .leftJoin(MemberUserDO.class,MemberUserDO::getId,SupplyOrderEvaluateDO::getFromUserId)
                    .leftJoin(SupplyOrderDO.class,SupplyOrderDO::getId,SupplyOrderEvaluateDO::getOrderId)
                    .leftJoin(BusinessUserDO.class,BusinessUserDO::getUserId, SupplyOrderEvaluateDO::getFromUserId)
                    .leftJoin(BusinessCreditDO.class,BusinessCreditDO::getId,BusinessUserDO::getCreditId)
                    .leftJoin(MemberTypeDO.class,MemberTypeDO::getId,BusinessUserDO::getMemberTypeId)
                    .selectAll(SupplyOrderEvaluateDO.class)
                    .selectAs(BusinessCreditDO::getIcon,AppSupplyOrderEvaluateRespVO::getIcon)
                    .selectAs(MemberUserDO::getNickname, AppSupplyOrderEvaluateRespVO::getNickname)
                    .selectAs(MemberUserDO::getAvatar,AppSupplyOrderEvaluateRespVO::getAvatar)
                    .selectAs(MemberUserDO::getMark,AppSupplyOrderEvaluateRespVO::getMark)
                    .selectAs(MemberTypeDO::getName, AppSupplyOrderEvaluateRespVO::getMemberTypeName)
                    .selectAs(MemberTypeDO::getIcon,AppSupplyOrderEvaluateRespVO::getMemberTypeIcon)
                    .selectAs(BusinessUserDO::getAreaId,AppSupplyOrderEvaluateRespVO::getAreaId)
                    //查询订单的卖方id，用于判断发出评价的人是买家还是卖家
                    .selectAs(SupplyOrderDO::getSaleUserId,AppSupplyOrderEvaluateRespVO::getSaleUserId)
                    .orderByDesc(SupplyOrderEvaluateDO::getCreateTime)
            );
        }

        //查询我收到的评价
        return selectJoinList(AppSupplyOrderEvaluateRespVO.class, new MPJLambdaWrapperX<SupplyOrderEvaluateDO>()
                .eq(SupplyOrderEvaluateDO::getToUserId, loginUserId)
                .leftJoin(MemberUserDO.class,MemberUserDO::getId,SupplyOrderEvaluateDO::getFromUserId)
                .leftJoin(SupplyOrderDO.class,SupplyOrderDO::getId,SupplyOrderEvaluateDO::getOrderId)
                .leftJoin(BusinessUserDO.class,BusinessUserDO::getUserId, SupplyOrderEvaluateDO::getFromUserId)
                .leftJoin(BusinessCreditDO.class,BusinessCreditDO::getId,BusinessUserDO::getCreditId)
                .leftJoin(MemberTypeDO.class,MemberTypeDO::getId,BusinessUserDO::getMemberTypeId)
                .selectAll(SupplyOrderEvaluateDO.class)
                .selectAs(BusinessCreditDO::getIcon,AppSupplyOrderEvaluateRespVO::getIcon)
                .selectAs(BusinessUserDO::getAreaId,AppSupplyOrderEvaluateRespVO::getAreaId)
                .selectAs(MemberUserDO::getNickname, AppSupplyOrderEvaluateRespVO::getNickname)
                .selectAs(MemberUserDO::getAvatar,AppSupplyOrderEvaluateRespVO::getAvatar)
                .selectAs(MemberUserDO::getMark,AppSupplyOrderEvaluateRespVO::getMark)
                .selectAs(MemberTypeDO::getName, AppSupplyOrderEvaluateRespVO::getMemberTypeName)
                .selectAs(MemberTypeDO::getIcon,AppSupplyOrderEvaluateRespVO::getMemberTypeIcon)
                .selectAs(SupplyOrderDO::getSaleUserId,AppSupplyOrderEvaluateRespVO::getSaleUserId)
                .orderByDesc(SupplyOrderEvaluateDO::getCreateTime)
        );
    }

    default AppSupplyOrderEvaluateRespVO getSupplyOrderEvaluate(Long id){
        return selectJoinOne(AppSupplyOrderEvaluateRespVO.class,new MPJLambdaWrapperX<SupplyOrderEvaluateDO>()
                .eq(SupplyOrderEvaluateDO::getId,id)
                .leftJoin(MemberUserDO.class,MemberUserDO::getId,SupplyOrderEvaluateDO::getFromUserId)
                .leftJoin(SupplyOrderDO.class,SupplyOrderDO::getId,SupplyOrderEvaluateDO::getOrderId)
                .leftJoin(BusinessUserDO.class,BusinessUserDO::getUserId, SupplyOrderEvaluateDO::getFromUserId)
                .leftJoin(BusinessCreditDO.class,BusinessCreditDO::getId,BusinessUserDO::getCreditId)
                .leftJoin(MemberTypeDO.class,MemberTypeDO::getId,BusinessUserDO::getMemberTypeId)
                .selectAll(SupplyOrderEvaluateDO.class)
                .selectAs(BusinessCreditDO::getIcon,AppSupplyOrderEvaluateRespVO::getIcon)
                .selectAs(MemberUserDO::getNickname, AppSupplyOrderEvaluateRespVO::getNickname)
                .selectAs(MemberUserDO::getAvatar,AppSupplyOrderEvaluateRespVO::getAvatar)
                .selectAs(MemberTypeDO::getName, AppSupplyOrderEvaluateRespVO::getMemberTypeName)
                .selectAs(MemberTypeDO::getIcon,AppSupplyOrderEvaluateRespVO::getMemberTypeIcon)

                //查询订单的卖方id，用于判断发出评价的人是买家还是卖家
                .selectAs(SupplyOrderDO::getSaleUserId,AppSupplyOrderEvaluateRespVO::getSaleUserId)
        );
    }


    /**
     * 获取当前用户的评价
     * @param orderId
     * @param fromUserId
     * @return
     */
    default SupplyOrderEvaluateDO selectByOrderId(long orderId,long fromUserId) {
        return selectOne(SupplyOrderEvaluateDO::getOrderId, orderId, SupplyOrderEvaluateDO::getFromUserId,fromUserId);
    }
}
